package cn.icanci.loopstack.ddk.admin.biz.service.impl;

import cn.hutool.json.JSONUtil;
import cn.icanci.loopstack.ddk.admin.biz.event.log.LogEvent;
import cn.icanci.loopstack.ddk.admin.biz.mapping.config.AppMapping;
import cn.icanci.loopstack.ddk.admin.biz.service.AppService;
import cn.icanci.loopstack.ddk.admin.biz.service.BaseService;
import cn.icanci.loopstack.ddk.admin.biz.utils.EnvUtils;
import cn.icanci.loopstack.ddk.admin.dal.mapper.entity.AppDO;
import cn.icanci.loopstack.ddk.admin.dal.mapper.mapper.AppMapper;
import cn.icanci.loopstack.ddk.common.enums.LogOperatorTypeEnum;
import cn.icanci.loopstack.ddk.common.enums.ModuleTypeEnum;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.model.TextValue;
import cn.icanci.loopstack.ddk.common.model.config.AppVO;
import cn.icanci.loopstack.ddk.common.utils.PaginatorUtils;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

/**
 * App 操作
 * 
 * @author icanci
 * @since 1.0 Created in 2023/01/06 09:44
 */
@Service
public class AppServiceImpl extends BaseService<AppVO> implements AppService {
    @Resource
    private AppMapper  appMapper;
    @Resource
    private AppMapping appMapping;

    @Override
    public PageList<AppVO> queryPage(AppVO vo, int currentPage, int pageSize) {
        vo.setEnv(getEnv());
        vo.setIsDelete(null);

        currentPage = Math.max(currentPage - 1, 0);

        AppDO ddkApp = appMapping.vo2do(vo);
        List<AppDO> ret = appMapper.selectPage(ddkApp, currentPage, pageSize);

        return PaginatorUtils.formatPaginator(appMapping.dos2vos(ret), currentPage, pageSize, appMapper.total(ddkApp));
    }

    @Override
    public void save(AppVO vo) {
        if (doInsert(vo)) {
            beforeInsert(vo);
            AppDO app = appMapping.vo2do(vo);
            appMapper.insert(app);
            eventDispatcher.fire(new LogEvent(vo.getUuid(), ModuleTypeEnum.DDK_APP, JSONUtil.toJsonStr(app), LogOperatorTypeEnum.CREATE), false);
        } else {
            beforeUpdate(vo);
            AppDO app = appMapping.vo2do(vo);
            appMapper.update(app);
            eventDispatcher.fire(new LogEvent(vo.getUuid(), ModuleTypeEnum.DDK_APP, JSONUtil.toJsonStr(app), LogOperatorTypeEnum.UPDATE), false);
        }
    }

    @Override
    public AppVO queryByAppId(String appId) {
        AppDO appDO = appMapper.queryByAppId(appId, EnvUtils.getEnv());
        return appMapping.do2vo(appDO);
    }

    @Override
    public AppVO queryByAppUuid(String appUuid) {
        AppDO appDO = appMapper.queryByAppUuid(appUuid, EnvUtils.getEnv());
        return appMapping.do2vo(appDO);
    }

    @Override
    public AppVO queryByAppName(String appName) {
        AppDO appDO = appMapper.queryByAppName(appName, EnvUtils.getEnv());
        return appMapping.do2vo(appDO);
    }

    @Override
    public List<TextValue> loadSelector() {
        AppDO appDO = new AppDO();
        appDO.setEnv(getEnv());

        List<AppDO> apps = appMapper.selectList(appDO);

        if (CollectionUtils.isEmpty(apps)) {
            return Lists.newArrayList();
        }

        List<AppVO> appVOS = appMapping.dos2vos(apps);
        List<TextValue> textValues = Lists.newArrayList();
        for (AppVO app : appVOS) {
            String label;
            if (isDeleted(app)) {
                label = String.format(DELETED_FORMAT, app.getAppName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, app.getAppName());
            }
            String value = app.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public List<TextValue> loadAppIdSelector() {
        AppDO appDO = new AppDO();
        appDO.setEnv(getEnv());

        List<AppDO> apps = appMapper.selectList(appDO);

        if (CollectionUtils.isEmpty(apps)) {
            return Lists.newArrayList();
        }

        List<AppVO> appVOS = appMapping.dos2vos(apps);
        List<TextValue> textValues = Lists.newArrayList();
        for (AppVO app : appVOS) {
            String label;
            if (isDeleted(app)) {
                label = String.format(DELETED_FORMAT, app.getAppName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, app.getAppName());
            }
            String value = app.getAppId();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }
}
